﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;

namespace ToolUtil
{
    public class StringUtil
    {
        /// <summary>
        /// list转mssql的in
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string ListToString(List<Object> list)
        {
            List<object> list1 = list.Distinct().ToList();
            string tname = string.Empty;
            foreach (object o in list1)
            {
                try
                {
                    Convert.ToInt64(o);
                    tname = "Long";
                }
                catch
                {
                    tname = "String";
                }
                break;
            }
            //Console.WriteLine("字迹的项目类型：" + tname);
            string ones = string.Empty;
            switch (tname)
            {
                case "Long":
                    ones = string.Join(",", list1.ToArray());
                    ones = "(" + ones + ")";
                    break;
                case "String":
                    ones = string.Join("'" + ",'", list1.ToArray());
                    ones = "('" + ones + "')";
                    break;
            }
            return ones;
        }
        /// <summary>
        /// string转mssql的in
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c">分隔符</param>
        /// <returns></returns>
        public static string stringTolist(string str, char c)
        {
            string[] strs = str.Split(c);
            List<object> list = new List<object>();
            foreach (object s in strs)
            {
                list.Add(s);
            }
            List<object> list1 = list.Distinct().ToList();
            string tname = string.Empty;
            foreach (object o in list1)
            {
                try
                {
                    Convert.ToInt64(o);
                    tname = "Long";
                }
                catch
                {
                    tname = "String";
                }
                break;
            }
            //Console.WriteLine("字迹的项目类型：" + tname);
            string ones = string.Empty;
            switch (tname)
            {
                case "Long":
                    ones = string.Join(",", list1.ToArray());
                    ones = "(" + ones + ")";
                    break;
                case "String":
                    ones = string.Join("'" + ",'", list1.ToArray());
                    ones = "('" + ones + "')";
                    break;
            }
            return ones;
        }

        static int[] iSign =   {65306,8220,8216,65292,65281,12289,65311,8212,12290,12298,12297,8230,65509,65288,65289,8217,8221};
        /// <summary>
        /// 判断是否为汉字
        /// </summary>
        public static bool IsHanzi(char ch)
        {
            int n = (int)ch;
            if (n <= 0X1ef3) // < 1024
                return false;
            foreach (int v in iSign)
            {
                if (ch == v)
                    return false;
            }
            return true;
        }
        public static string SpecialChars = "！·＃￥％……—＊（）——＋－＝［］《》＜＞，。？／＼｜｛｝“”‘’•";
        /// <summary>
        /// 获取字符宽度
        /// </summary>
        public static int CharWidth(string str)
        {
            int charWidth = 0;
            using (Bitmap image = new Bitmap((int)Math.Ceiling(str.Length * 9.5), 22))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                    System.Drawing.SizeF sizef = g.MeasureString(str, font);
                    charWidth = (int)sizef.Width;
                }

                return charWidth;
            }
        }
        /// <summary>
        /// 截取汉字字符串
        /// </summary>
        public static string SubStrByByte(string str, int start, int length)
        {
            int len = length;
            int byteCount = System.Text.Encoding.Default.GetByteCount(str);
            //修改最大长度，防止溢出
            if (len > byteCount)
            {
                len = byteCount;
            }
            var strBytes = System.Text.Encoding.Default.GetBytes(str);
            string substr = System.Text.Encoding.Default.GetString(strBytes, start, len);
            //对于半个中文字符的特殊处理
            if (substr.EndsWith("?"))
            {
                //判断原字符串是否包含问号
                var c = str.Substring(substr.Length - 1, 1);
                if (!c.Equals("?"))
                {
                    substr = substr.Substring(0, substr.Length - 1);
                }
            }
            return substr + "...";
        }
        public static string SubStrByte(string str, int start, int length)
        {
            int len = length;
            int byteCount = System.Text.Encoding.Default.GetByteCount(str);
            //修改最大长度，防止溢出
            if (len > byteCount)
            {
                len = byteCount;
            }
            var strBytes = System.Text.Encoding.Default.GetBytes(str);
            string substr = System.Text.Encoding.Default.GetString(strBytes, start, len);
            //对于半个中文字符的特殊处理
            if (substr.EndsWith("?"))
            {
                //判断原字符串是否包含问号
                var c = str.Substring(substr.Length - 1, 1);
                if (!c.Equals("?"))
                {
                    substr = substr.Substring(0, substr.Length - 1);
                }
            }
            return substr;
        }
        /// <summary>
        /// 获取字符串长度
        /// </summary>
        /// <param name="strdata"></param>
        /// <returns></returns>
        public static int GetStrLength(string strdata)
        {
            int num = 0;
            char[] chArray = strdata.ToCharArray();
            for (int i = 0; i <= (chArray.Length - 1); i++)
            {
                num += GetCharLength(chArray[i]);
            }
            return num;
        }
        private static int GetCharLength(char ch)
        {
            int num = 0;
            if ((((short)ch) < 0) || (((short)ch) > 0x80))
            {
                return (num + 2);
            }
            num++;
            return num;
        }
        public static int GetNextNumber(List<int> list)
        {
            for (int i = 0, j = 1; j < list.Count - 1; i++, j++)
            {
                if (list[i] - j > 0)
                    return j;
                //如果出现断号，则补齐断号
                if ((list[j] - list[i]) > 1)
                {
                    return list[i] + 1;
                }
            }
            return list[list.Count - 1] + 1;
        }
        /// <summary>
        /// 字符串提取单价
        /// </summary>
        /// <param name="ydj"></param>
        /// <returns></returns>
        public static double GetDj(string ydj)
        {
            string dj = "";
            if (!string.IsNullOrEmpty(ydj))
            {
                if (ydj.StartsWith("CNY"))
                    ydj = ydj.Replace("CNY", "");
                if (ydj.Contains("("))
                    ydj = ydj.Substring(0, ydj.IndexOf("("));
                dj = Regex.Replace(ydj, @"[^\d.\d]", "");
            }
            double dj1 = 0.00;
            if (!string.IsNullOrEmpty(dj))
            {
                try
                {
                    dj1 = double.Parse(dj);
                }
                catch { }
            }
            return dj1;
        }
        /// <summary>
        /// 去除HTML标签
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }
        /// <summary>
        /// 13isbn转10
        /// </summary>
        /// <param name="barCode"></param>
        /// <returns></returns>
        public static string BarCodeToISBN(string barCode)
        {
            barCode = barCode.Replace("-", "");
            if (barCode.Length != 13)
            {
                return barCode;
            }
            if (!Regex.IsMatch(barCode, @"\d{13}"))
            {
                return barCode;
            }
            string str = barCode.Substring(3, 9);
            int num = 0;
            for (int i = 10; i >= 2; i--)
            {
                num += i * Convert.ToInt32(str.Substring(10 - i, 1));
            }
            string str2 = null;
            if ((num % 11) == 0)
            {
                str2 = "0";
            }
            else if ((num % 11) == 1)
            {
                str2 = "X";
            }
            else
            {
                str2 = (11 - (num % 11)).ToString();
            }
            return (str + str2);
        }
        /// <summary>
        /// 10位ISBN加-
        /// </summary>
        /// <param name="strISBNCode"></param>
        /// <returns></returns>
        public static string BuingISBNSept(string strISBNCode)
        {
            string str;
            if (strISBNCode.Length != 10)
            {
                return strISBNCode;
            }
            string str2 = strISBNCode.Substring(1, 1);
            if (str2 == "0")
            {
                str = strISBNCode.Substring(0, 1) + "-" + strISBNCode.Substring(1, 2) + "-" + strISBNCode.Substring(3, 6) + "-" + strISBNCode.Substring(9, 1);
            }
            else
            {
                if ((Convert.ToInt32(str2) >= 1) && (Convert.ToInt32(str2) <= 4))
                {
                    return (strISBNCode.Substring(0, 1) + "-" + strISBNCode.Substring(1, 3) + "-" + strISBNCode.Substring(4, 5) + "-" + strISBNCode.Substring(9, 1));
                }
                if ((Convert.ToInt32(str2) >= 5) && (Convert.ToInt32(str2) <= 7))
                {
                    return (strISBNCode.Substring(0, 1) + "-" + strISBNCode.Substring(1, 4) + "-" + strISBNCode.Substring(5, 4) + "-" + strISBNCode.Substring(9, 1));
                }
                if (str2 == "8")
                {
                    str = strISBNCode.Substring(0, 1) + "-" + strISBNCode.Substring(1, 5) + "-" + strISBNCode.Substring(6, 3) + "-" + strISBNCode.Substring(9, 1);
                }
                else
                {
                    str = strISBNCode.Substring(0, 1) + "-" + strISBNCode.Substring(1, 6) + "-" + strISBNCode.Substring(7, 2) + "-" + strISBNCode.Substring(9, 1);
                }
            }
            return str;
        }
        /// <summary>
        /// 全角转半角
        /// 全角空格为12288，半角空格为32
        /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        /// </summary>
        /// <param name="input">输入包含全角字符的字符串</param>
        /// <returns>返回半角字符串</returns>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32; continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }
        /// <summary>
        /// 提取登录号
        /// </summary>
        public static List<string> getDlhs(string dlh)
        {
            List<string> list = new List<string>();
            if (!string.IsNullOrEmpty(dlh))
            {
                string[] arry = dlh.Split(';');
                foreach (string str in arry)
                {
                    string[] arry1 = str.Split('-');
                    if (arry1.Length == 1)
                    {
                        list.Add(str);
                    }
                    else if (arry1.Length == 2)
                    {
                        string yd = arry1[0];
                        string ed = arry1[1];
                        if (string.IsNullOrEmpty(yd))
                            ed = ed.Replace("-", "");
                        else if (yd.Length - ed.Length > 0)
                            ed = yd.Substring(0, yd.Length - ed.Length) + ed;
                        //提取纯数字，该方式会将所有数字提取出来并拼接在一起，如："ABC#123@AS456测试"提取出来就是123456
                        //var yd1 = Regex.Replace(yd, @"[^0-9]+", "");
                        //var ed1 = Regex.Replace(ed, @"[^0-9]+", "");
                        //int cs= int.Parse(ed1) - int.Parse(yd1) + 1;
                        string y1="0", y2="0";
                        var reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        var mc = reg.Matches(yd);
                        foreach (Match m in mc)
                        {
                            //将会返回123、456、789
                            var val = m.Groups[0].Value;
                            y1 = val;
                        }
                        var mc1 = reg.Matches(ed);
                        foreach (Match m in mc1)
                        {
                            var val = m.Groups[0].Value;
                            y2 = val;
                        }
                        //Console.WriteLine(y1+":"+y2+":"+ int.Parse(y2));
                        long cs = long.Parse(y2) - long.Parse(y1) + 1;
                        int cd = yd.LastIndexOf(long.Parse(y1).ToString()) + long.Parse(y1).ToString().Length;
                        //int n = 0;
                        //int w= yd.TakeWhile(c => (n -= (c == Convert.ToChar(long.Parse(y1).ToString()) ? 1 : 0)) > 0).Count();
                        string qbf = yd.Substring(0, yd.LastIndexOf(long.Parse(y1).ToString()));
                        string hbf = yd.Substring(cd);
                        //Console.WriteLine(cs + ":" + qbf+":"+hbf);
                        for (int i=0; i<cs; i++)
                        {
                            string s = (long.Parse(y1) + i).ToString();
                            string qb = qbf + s + hbf;
                            if(s.Length> long.Parse(y1).ToString().Length)
                                qb= qbf.Substring(0,qbf.Length-1) + s + hbf;
                            list.Add(qb);
                            //Console.WriteLine(qb);
                        }
                    }
                }
            }
            return list;
        }
    }
}
